/**
 * @author Tobias Boehm
 *
 * 24.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.server.DefaultTBSserverMessageFlags;

/**
 * This server message should be used to inform the requesting player about
 * fail or success of his slot change request.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerMatchpreparationSlotChangeRequestReplyMessage extends
		AbsServerMatchpreparationSlotMessage implements DefaultTBSserverMessageFlags, ICopyable<ServerMatchpreparationSlotChangeRequestReplyMessage> {

	public static enum SlotChangeReplyReason{OK,CHANGE_IMPOSSIBLE,NO_PERMISSION,SLOT_DOES_NOT_EXIST,MATCH_DOES_NOT_EXIST}
	
	private boolean mAccepted = false;
	private SlotChangeReplyReason mReason = SlotChangeReplyReason.NO_PERMISSION;
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCHPREPARATION_SLOT_CHANGE_REQUEST_REPLY;
	}

	@Override
	protected void onReadTransmissonMatchSlotData(
			DataInputStream pDataInputStream) throws IOException {
		
		this.mAccepted = pDataInputStream.readBoolean();
		
		this.mReason = SlotChangeReplyReason.valueOf(pDataInputStream.readUTF());
	}

	@Override
	protected void onWriteTransmissonMatchSlotData(
			DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeBoolean(this.mAccepted);
		
		pDataOutputStream.writeUTF(this.mReason.name());
	}

	/**
	 * @return the mAccepted
	 */
	public boolean isAccepted() {
		return mAccepted;
	}

	/**
	 * @param mAccepted the mAccepted to set
	 */
	public void setAccepted(boolean mAccepted) {
		this.mAccepted = mAccepted;
	}

	/**
	 * @return the mReason
	 */
	public SlotChangeReplyReason getReason() {
		return mReason;
	}

	/**
	 * @param mReason the mReason to set
	 */
	public void setReason(SlotChangeReplyReason mReason) {
		this.mReason = mReason;
	}

	@Override
	public ServerMatchpreparationSlotChangeRequestReplyMessage copy() {
		
		final ServerMatchpreparationSlotChangeRequestReplyMessage copy = new ServerMatchpreparationSlotChangeRequestReplyMessage();
		
		copy.mAccepted = this.mAccepted;
		copy.mMatchID = this.mMatchID;
		copy.mReason = this.mReason;
		copy.mSlotID = this.mSlotID;
		
		return copy;
	}

}
